import  threading
import time
boymutex=threading.Lock() #创建一个锁
girlmutex=threading.Lock() #创建一个锁

class  boy(threading.Thread):
    def run(self):
        if  boymutex.acquire(1): #锁定成功就继续执行，锁定不成功，就一直等待
            print(self.name+"boy  say i  am sorry   up")
            time.sleep(3)

            if  girlmutex.acquire(1): #锁定不成功。一直等待
                print(self.name+"boy  say i  am sorry   down")
                girlmutex.release()
            boymutex.release()

class  girl(threading.Thread):
    def run(self):
        if  girlmutex.acquire(1):#锁定成功就继续执行，锁定不成功，就一直等待
            print(self.name+"girl say i  am sorry  up")
            time.sleep(3)

            if boymutex.acquire(1): #锁定不成功。一直等待
                print(self.name+"girl say i  am sorry  down")
                boymutex.release()
            girlmutex.release()



#开启两个线程
boy1=boy()
boy1.start()
girl1=girl()
girl1.start()
